﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class Admin_MaintainExhibitions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();            
        }
        if (Request["ExhibitionID"] != null || Request["maintain"] != null)
        {
            Control ct;
            ct = Page.LoadControl("~/Share/Insert_Update_ExhibitionControl.ascx");
            pn_Exhibition.Controls.Clear();
            pn_Exhibition.Controls.Add(ct);
        }
    }

    public void loadData()
    {
        ExhibitionDAO exhibitionDAO = new ExhibitionDAO();
        grv_Exhibitions.DataSource = exhibitionDAO.GetAll();
        grv_Exhibitions.DataBind();
    }


    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/MaintainExhibitions.aspx?maintain=Insert");
    }

    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grv_Exhibitions.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grv_Exhibitions.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = false;
            }
        }




    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    
    {

            try
            {
                ExhibitionDAO exhDAO = new ExhibitionDAO();
                int id = 0;
                bool ischeck = false;

                foreach (GridViewRow row in grv_Exhibitions.Rows)
                {
                    if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                    {
                        // ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true);
                        ischeck = true;
                    }

                }

                if (ischeck == true)
                {
                    foreach (GridViewRow row in grv_Exhibitions.Rows)
                    {
                        if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                        {
                            id = int.Parse(grv_Exhibitions.DataKeys[row.RowIndex].Value.ToString());
                            if (exhDAO.DeleteExhibition(id) > 0)
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete successfull!');", true);
                            }
                            else
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);
                        }

                    }
                    loadData();
                }
                else { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true); }
            }
            catch (Exception ex) { }


        }
    
    
    protected void grv_Exhibitions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Exhibitions.PageIndex = e.NewPageIndex;
        loadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ExhibitionDAO exhibitionDAO = new ExhibitionDAO();
        grv_Exhibitions.DataSource = exhibitionDAO.GetByName("%"+txtSearch.Text+"%");
        grv_Exhibitions.DataBind();
    }
}
